<?php


namespace App\Http\Controllers\ums;

use App\Common\Components\ApiController;
use App\Models\Admin;
use App\Models\AdminRoleRelation;
use App\Models\RoleMenuRelation;
use App\Models\Role;
use Illuminate\Support\Facades\DB;

class AdminController extends ApiController
{
    //用户列表
    public function list(){
        $param = request()->input();
        $record = Admin::paginate(10);
        return $this->successJson('操作成功',$record);
    }

    public function role(){
        $param = request()->input();
        $admin_id = $param['id'];
        $AdminRoleRelation = AdminRoleRelation::where('admin_id',$admin_id)->first();
        $record = [];
        if($AdminRoleRelation){
            $roleIds = explode(",", $AdminRoleRelation->role_ids);
            foreach($roleIds as $roleId){
            $record[] = Role::where('id',$roleId)->first();
        }
        }
        return $this->successJson('操作成功',$record);
    }

    public function updateStatus(){
        $param = request()->input();
        $admin_id = $param['id'];
        $status = $param['status'];
        $admin = Admin::where('id',$admin_id)->first();
        $admin->status = $status;
        $admin->save();
        return $this->successJson();
    }

    public function updatePwd(){
        $param = request()->input();
        $admin_id = $param['id'];
        $password = $param['password'];
        $hash = password_hash($password, PASSWORD_DEFAULT);
        $admin = Admin::where('id',$admin_id)->first();
        $admin->password = $hash;
        $admin->save();
        return $this->successJson();
    }

    public function update(){
        $param = request()->input();
        $admin_id = $param['id'];
        $admin = Admin::where('id',$admin_id)->first();
        $admin->username = $param['username'];
        $admin->icon = $param['icon'];
        $admin->username = $param['username'];
        $admin->email = $param['email'];
        $admin->nick_name = $param['nick_name'];
        $admin->note = $param['note'];
        $admin->status = $param['status'];
        $admin->save();
        return $this->successJson();
    }

    public function login(){
        $param = request()->input();
        $username = $param['username'];
        $password = $param['password'];
        $admin = Admin::where('username',$username)->first();
        $now = new \DateTime();
        $admin->login_time = $now;
        $isPasswordCorrect = password_verify($password, $admin->password);
        if($isPasswordCorrect){
            $data['icon'] = $admin->icon;
            $data['username'] = $admin->username;
            $admin->save();
            return $this->successJson('',$data);
        }
        return $this->errorJson();
    }


    public function register(){
        $param = request()->input();
        $admin = new Admin();
        $admin->username = $param['username'];
        $now = new \DateTime();
        $admin->create_time = $now;
        $record = Admin::where('username',$admin->username)->first();
        if($record){
            return $this->errorJson('用户已存在');
        }
        $admin->fill($param);
        $admin->save();
        return $this->successJson();
    }

    public function delete(){
        $param = request()->input();
        $admin_id = $param['id'];
        Admin::where('id', $admin_id)->delete();
        return $this->successJson();
    }

    public function roleUpdate(){
        $param = request()->input();
        $admin_id = $param['adminId'];
        $AdminRoleRelation = AdminRoleRelation::where('admin_id', $admin_id)->first();
        if($AdminRoleRelation){
            $AdminRoleRelation->role_ids = $param['roleIds'];
            $AdminRoleRelation->save();
        }else{
            AdminRoleRelation::create([
                'admin_id' => $admin_id,
                'role_ids' =>$param['roleIds'],
            ]);
        }
        return $this->successJson();
    }

    public function info(){
        $param = request()->input();
        $username = $param['username'];
        $Admin = Admin::where('username',$username)->first();
        $AdminRoleRelation = AdminRoleRelation::where('admin_id', $Admin->id)->first();
        if(!$AdminRoleRelation){
            return $this->successJson();
        }
        $roleIds = explode(",", $AdminRoleRelation->role_ids);
        $menus = [];
        foreach($roleIds as $roleId){
            $RoleMenuRelation = RoleMenuRelation::where('role_id', $roleId)->first();
            if($RoleMenuRelation){
                $menuCodes = explode(",", $RoleMenuRelation->menu_code);
                $menus = array_merge($menus,$menuCodes);
            }
        }
        $data['roles'] = $roleIds;
        $data['menus'] = $menus;
        $data['username'] = $username;
        $data['user'] = $Admin;
        return $this->successJson('',$data);
    }
}